﻿using System;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class admin_formulario : System.Web.UI.Page
{
    public ArrayList campos_arr = null;
    protected void Page_Load(object sender, EventArgs e)
    {
        Hashtable ht = null;
        int id=0;
        if (Session["userid"] == null) Response.Redirect("../default.aspx");

        if (Request.QueryString["id"] != null && !Request.QueryString["id"].ToString().Equals("")) { 
            id=int.Parse(Request.QueryString["id"].ToString());
            lb_id.Text = id.ToString();
            ht = (Hashtable)DB.getDatabyID(id);
        }


        ArrayList sections = DB.getSections(2);
        TableRow tr=null;
        TableCell tc1=null, tc2=null, tc3=null, tc4=null, tc5=null, tc6=null;
        Label lb_nombre=null;
        TextBox tb=null;
        DropDownList ddl=null;
        Label lb = null;
        RequiredFieldValidator rfv = null;
        RegularExpressionValidator rev = null;
        ListItem item= null;
        ArrayList listado_arr = null;
        int contador = 0;
        TableHeaderCell tdtitulo = null;
        Label lbtitulo = null;
        foreach (RE_GenericBean section in sections)
        {
            tr = new TableRow();
            lbtitulo = new Label();
            lbtitulo.Text = section.strC1;
            tdtitulo = new TableHeaderCell();
            //tdtitulo.Style.Add("style", "font-weight: bold; color: white; background-color: #6b696b; height: 13px;");
            tdtitulo.ColumnSpan = 6;
            tdtitulo.Controls.Add(lbtitulo);
            tr.Cells.Add(tdtitulo);
            Tabla.Rows.Add(tr);
            campos_arr = DB.getCamposFormulario(section.intC1.ToString());
            contador = 0;
            foreach (campos_Bean campo in campos_arr)
            {
                if (contador % section.intC2 == 0) tr = new TableRow();
                contador++;
                tc1 = new TableCell();
                tc2 = new TableCell();
                tc3 = new TableCell();
                tc4 = new TableCell();
                tc5 = new TableCell();
                tc6 = new TableCell();

                tc1.HorizontalAlign = HorizontalAlign.Left;
                tc2.HorizontalAlign = HorizontalAlign.Left;
                tc3.HorizontalAlign = HorizontalAlign.Left;
                tc4.HorizontalAlign = HorizontalAlign.Left;
                tc5.HorizontalAlign = HorizontalAlign.Left;
                tc6.HorizontalAlign = HorizontalAlign.Left;

                lb_nombre = new Label();
                lb_nombre.ID = "lb_" + campo.Nombre;
                lb_nombre.Text = campo.DisplayName;

                tc1.Controls.Add(lb_nombre);
                if (campo.TipoCampo.Equals("DropDown"))
                {
                    ddl = new DropDownList();
                    ddl.ID = campo.Nombre;
                    listado_arr = (ArrayList)DB.getListado(campo.TablaDep, campo.CampoDep, campo.WhereDep);
                    if ((listado_arr != null) && (listado_arr.Count > 0))
                    {
                        item = new ListItem("Select One", "");
                        ddl.Items.Add(item);
                        foreach (RE_GenericBean rgb in listado_arr)
                        {
                            item = new ListItem(rgb.strC1, rgb.strC1);
                            ddl.Items.Add(item);
                        }
                    }
                   
                    tc2.VerticalAlign = VerticalAlign.Middle;
                    tc2.Controls.Add(ddl);
                    if (ht != null)
                    {
                        if (ht.ContainsKey(ddl.ID.ToString()))
                            ddl.SelectedValue = ht[ddl.ID.ToString()].ToString();
                    }
                    if ((campo.Nombre.Equals("frmPSBPO_ClientE")))
                    {
                        ddl.SelectedValue = "Spark Energy";

                    }
                }
                else
                {
                    if (campo.TipoCampo.Equals("Label"))
                    {
                        lb = new Label();
                        lb.ID = campo.Nombre;
                        if ((campo.Nombre.Equals("frmPromo")))
                        {
                            lb.Text = "HMCDR02";

                        }
                        if ((campo.Nombre.Equals("frmIpAddress")))
                        {
                            lb.Text = "190.143.139.146";

                        }
                        lb.Width = campo.Size;
                        tc2.VerticalAlign = VerticalAlign.Middle;
                        tc2.Controls.Add(lb);
                       /* if (ht != null)
                        {
                            if (ht.ContainsKey(ddl.ID.ToString()))
                         //       lb.Text = ht[lb.ID.ToString()].ToString();
                        }*/
                    }
                    else
                    {
                        tb = new TextBox();
                        tb.ID = campo.Nombre;
                        if ((campo.Nombre.Equals("frmComments")) || (campo.Nombre.Equals("frmSurvey16")) || (campo.Nombre.Equals("frmSurvey18")) || (campo.Nombre.Equals("frmSurvey25")) || (campo.Nombre.Equals("frmQCComments")))
                        {
                            tb.TextMode = TextBoxMode.MultiLine;
                            tb.Rows = 7;
                        }
                        
                        tb.Width = campo.Size;
                        tb.MaxLength = campo.MaxLen;
                        tc2.VerticalAlign = VerticalAlign.Middle;
                        tc2.Controls.Add(tb);
                        if (ht != null)
                        {
                            if (ht.ContainsKey(tb.ID.ToString()))
                                tb.Text = ht[tb.ID.ToString()].ToString();
                        }
                        if (campo.AllowNull == 0)
                        {
                            rfv = new RequiredFieldValidator();
                            rfv.ID = "rfv_" + campo.Nombre;
                            rfv.ErrorMessage = "This field is required";
                            rfv.ControlToValidate = campo.Nombre;
                            rfv.SetFocusOnError = true;
                            tc2.Controls.Add(rfv);
                        }
                        if (campo.ValidaExpr == 1)
                        {
                            rev = new RegularExpressionValidator();
                            rev.ID = "rev_" + campo.Nombre;
                            if (campo.MessageError.Equals(""))
                                rev.ErrorMessage = "Incorrect Format";
                            else
                            {
                                rev.ErrorMessage = campo.MessageError;
                            }
                            rev.ControlToValidate = campo.Nombre;
                            rev.ValidationExpression = campo.Expresion_Reg;
                            tc2.Controls.Add(rev);
                        }
                    }
                }
                tr.Cells.Add(tc1);
                tr.Cells.Add(tc2);
                Tabla.Rows.Add(tr);
            }
        }
    }
    protected void btn_save_search_Click(object sender, EventArgs e)
    {
        int respuesta = Guardar_datos();
        if (respuesta == -1)
        {
            msg.Text = "Error, try again please";
            return;
        }
        Response.Redirect("buscador.aspx");
    }

    private int Guardar_datos() {
        int result = 0;
        TextBox tb = null;
        DropDownList ddl = null;
        string xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
        xml += "<campos>";
       
        foreach (Control tabla in Tabla.Controls) {
            foreach (Control rows in tabla.Controls)
            {
                foreach (Control child in rows.Controls)
                {
                    
                    if (child is TextBox)
                    {
                        tb = (TextBox)child;
                        if (tb.Text != null && !tb.Text.Trim().Equals(""))
                            xml += "<campo name=\"" + tb.ID + "\">" + tb.Text.ToUpper() + "</campo>";
                        else
                            xml += "<campo name=\"" + tb.ID + "\" />";
                    }
                    else if (child is DropDownList) {
                        ddl = (DropDownList)child;
                        xml += "<campo name=\"" + ddl.ID + "\">" + ddl.SelectedValue + "</campo>";
                    }

                }
            }
        }
        xml += "</campos>";
        int id = int.Parse(lb_id.Text);

        return DB.InsetUpdateData(id, xml);
    }
    protected void bt_delete_Click(object sender, EventArgs e)
    {
        int id = int.Parse(lb_id.Text);
        if (id == 0) {
            msg.Text = "Error.";
            msg.Visible = true;
            return;
        }

        int result = DB.deleteData(id);
        if (result != 1) {
            msg.Text = "Error.  Can't delete this item, please try again";
            msg.Visible = true;
            return;
        }
        Response.Redirect("buscador.aspx");
    }
}
